﻿B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Activity
Version=7.3
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: False
#End Region

Sub Process_Globals

End Sub

Sub Globals
	Dim txtUserID As EditText
	Dim txtPassword As EditText
	Dim txtFullName As EditText
	Dim txtLocation As EditText 
	Dim txtEmail As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("frmRegister")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnRegister_Click
	Dim strUserID As String = txtUserID.Text.Trim
	If strUserID = "" Then
		Msgbox("请输入用户名", "错误")
		Return
	End If	
	Dim strPassword As String = txtPassword.Text.Trim 
	If strPassword = "" Then
		Msgbox("请输入密码", "错误")
		Return
	End If
	Dim strFullName As String = txtFullName.Text.Trim
	If strFullName = "" Then
		Msgbox("请输入全名", "错误")
		Return
	End If
	Dim strLocation As String = txtLocation.Text.Trim
	If strLocation = "" Then
		Msgbox("请输入位置", "错误")
		Return
	End If
	Dim strEmail As String = txtEmail.Text.Trim
	If strEmail = "" Then
		Msgbox("请输入邮箱", "错误")
		Return
	End If	
	If Validate_Email(strEmail) = False Then
		Msgbox("邮箱格式错误", "错误")
		Return
	End If	
	
    Dim Job1 As HttpJob
	Job1.Initialize("Register", Me)
	Job1.Download2(Main.strURL & "signup.php", _
      Array As String("Action", "Register", _
	  "UserID", txtUserID.Text, _
	  "Password", txtPassword.Text, _
	  "FullName", txtFullName.Text, _
	  "Location", txtLocation.text, _
	  "Email", txtEmail.Text))
	ProgressDialogShow("正在连接到服务器...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
		Dim parser As JSONParser
    	Dim res As String
		Dim action As String
    	res = Job.GetString		
        parser.Initialize(res)                
        Select Job.JobName
            Case "Register"
                action = parser.NextValue
                If action = "Mail" Then
					Msgbox("电子邮件已发送到 " & txtEmail.Text & ". 请点击链接完成注册!", "注册")
					Activity.Finish
				Else If action = "MailInUse" Then
					Msgbox("用户名 '" & txtUserID.Text & "' 或者 (" & txtEmail.Text & ") 已在使用中", "注册")
				Else
					Msgbox("服务器返回一个错误.", "注册")
                End If
		End Select
	Else
		'Log("错误: " & Job.ErrorMessage)
		ToastMessageShow("错误: " & Job.ErrorMessage, True)
	End If
	Job.Release	
End Sub

' // Source: http://www.b4x.com/android/forum/threads/validate-a-correctly-formatted-email-address.39803/
Sub Validate_Email(EmailAddress As String) As Boolean
    Dim MatchEmail As Matcher = Regex.Matcher("^(?i)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])$", EmailAddress)
 
    If MatchEmail.Find = True Then
        'Log(MatchEmail.Match)
        Return True
    Else
		'Log("噢，请再检查一下你的电子邮件地址...")
        Return False
    End If
End Sub